package com.myProject.control.defence;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TArea;
import com.myProject.bean.TClass;
import com.myProject.serves.AreaSev;
import com.myProject.serves.ClassSev;
import com.myProject.serves.Impl.AreaSevImpl;
import com.myProject.serves.Impl.ClassSevImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ClassServlet", urlPatterns = "/classManage")
public class ClassServlet extends HttpServlet {
    ClassSev cs = new ClassSevImpl();
    AreaSev as = new AreaSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:20
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            addClass(request, response);
        } else if ("alt".equalsIgnoreCase(med)) {
            altClass(request, response);
        } else if ("showNoClass".equalsIgnoreCase(med)) {
            showNoClass(request, response);
        } else if ("showAllClass".equalsIgnoreCase(med)) {
            showAllClass(request, response);
        }
    }

    private void showAllClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TClass tt = new TClass();
        List<TClass> list = cs.getClassList(tt);
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(list);
        System.out.println("str = " + str);
        response.getWriter().write(str);
    }

    private void showNoClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<TArea> list = as.findNoClass();
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(list);
        System.out.println("str = " + str);
        response.getWriter().write(str);
    }

    private void altClass(HttpServletRequest request, HttpServletResponse response) {
        int tel = Integer.parseInt(request.getParameter("tel"));
        String people = request.getParameter("people");
        int num = Integer.parseInt(request.getParameter("num"));
        int pkId = Integer.parseInt(request.getParameter("pkId"));
        TClass tClass = new TClass();
        tClass.setTel(tel);
        tClass.setPeople(people);
        tClass.setNum(num);
        tClass.setPkId(pkId);
        cs.updateClass(tClass);
    }


    private void addClass(HttpServletRequest request, HttpServletResponse response) {
        int tel = Integer.parseInt(request.getParameter("tel"));
        String className = request.getParameter("className");
        String people = request.getParameter("people");
        int num = Integer.parseInt(request.getParameter("num"));
        String area = request.getParameter("area");

        TClass tClass = new TClass();
        tClass.setClassName(className);
        tClass.setPeople(people);
        tClass.setNum(num);
        tClass.setTel(tel);
        tClass.setArea(area);
        cs.addClass(tClass);
        tClass.getPkId();

        TArea tArea = new TArea();
        tArea.settClass(tClass);
        as.updateArea(tArea);

    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        String areaName = request.getParameter("areaName");
        String className = request.getParameter("className");
        TClass tClass = new TClass();
        tClass.setArea(areaName);
        tClass.setClassName(className);

        PageInfo<TClass> show = cs.showClassByPage(pageNo, pageSize, tClass);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);

    }
}
